package com.icedevcloud.upms.controller.admin.dept;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.icedevcloud.common.core.api.R;
import com.icedevcloud.common.core.log.IceLog;
import com.icedevcloud.upms.controller.admin.dept.dto.AddDeptReqDto;
import com.icedevcloud.upms.controller.admin.dept.dto.DeptQueryReqDto;
import com.icedevcloud.upms.controller.admin.dept.dto.DeptSimpleResDto;
import com.icedevcloud.upms.controller.admin.dept.dto.UpdateDeptReqDto;
import com.icedevcloud.upms.entity.SysDept;
import com.icedevcloud.upms.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2023-08-13
 */
@Tag(name = "部门接口类")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {

    private final ISysDeptService iSysDeptService;

    @IceLog
    @SaCheckPermission("upms:dept:page")
    @Operation(summary = "部门列表接口")
    @PostMapping("/deptList")
    public R<List<SysDept>> deptList(@RequestBody @Validated DeptQueryReqDto reqDto) {
        List<SysDept> result = iSysDeptService.deptList(reqDto);
        return R.ok(result);
    }

    @IceLog
    @Operation(summary = "部门列表接口")
    @PostMapping("/dictSimpleList")
    public R<List<DeptSimpleResDto>> dictSimpleList(@RequestBody @Validated DeptQueryReqDto reqDto) {
        List<SysDept> result = iSysDeptService.deptList(reqDto);
        List<DeptSimpleResDto> deptSimpleResDtos = BeanUtil.copyToList(result, DeptSimpleResDto.class);
        return R.ok(deptSimpleResDtos);
    }

    @IceLog
    @SaCheckPermission("upms:dept:add")
    @Operation(summary = "添加部门接口")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody @Validated AddDeptReqDto reqDto) {
        Boolean result = iSysDeptService.add(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:dept:update")
    @Operation(summary = "更新部门接口")
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UpdateDeptReqDto reqDto) {
        Boolean result = iSysDeptService.update(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:dept:delete")
    @Operation(summary = "删除部门接口")
    @DeleteMapping("/delete")
    public R<Boolean> delete(@NotNull(message = "ID不能为空") Long id) {
        Boolean result = iSysDeptService.delete(id);
        return R.ok(result);
    }

}
